<?php
include_once 'ns_include.php';
class application_factory
{    
    public static function handle(command $cmd)
    {
        logger::called();
        $sub_app = null;

        switch ($cmd->Protocol())
        {
            case LoginHandler::$PROTOCOL:
                $sub_app = LoginHandler::create($cmd);
                break;

            case Logout::$PROTOCOL:
                $sub_app = Logout::create($cmd);
                break;

            case CreateSoftwareDescription::$PROTOCOL: 
                $sub_app = CreateSoftwareDescription::create($cmd);
                break;

            case RequestChat::$PROTOCOL:
                $sub_app = RequestChat::create($cmd);
                break;

            case SendChatMessage::$PROTOCOL:
                $sub_app = SendChatMessage::create($cmd);
                break;

            case RequestStoreSoftwareList::$PROTOCOL:
                $sub_app = RequestStoreSoftwareList::create($cmd);
                break;

            case RequestUserSoftwareList::$PROTOCOL:
                $sub_app = RequestUserSoftwareList::create($cmd);
                break;

            case CheckSoftwareUpdate::$PROTOCOL:
                $sub_app = CheckSoftwareUpdate::create($cmd);
                break;

            case DownloadSoftware::$PROTOCOL:
                $sub_app = DownloadSoftware::create($cmd);
                break;

            case RequestVersionUpload::$PROTOCOL:
                $sub_app = RequestVersionUpload::create($cmd);
                break;

            case SoftwareUploaded::$PROTOCOL:
                $sub_app = SoftwareUploaded::create($cmd);
                break;

            default:                 
                break;
        }

        if(is_null($sub_app)){
            $cmd->SetResult("Unknown protocol ... '" . $cmd->Protocol() . "'");
            return;
        }

        if ($sub_app->requiresAuthentication())
            //if (!CheckAuthentication::isOk($cmd)) $sub_app = Logout::create($cmd); //If fails set protocol to logout
        
        $sub_app->execute();
    }    
}

?>
